package zoo.member.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import zoo.member.domain.MemberCommand;
import zoo.member.domain.MemberMilegeCommand;
import zoo.member.dao.MemberDao;

@Controller
public class MemberPaymentMilegeController {
	
	private String formViewName = "paymentmilege";
	private MemberDao memberDao;
	@Autowired
	public void setMemberDao(MemberDao memberDao) {
		this.memberDao = memberDao;
	}
	@RequestMapping(value="/main/paymentmilege.do", method=RequestMethod.GET)
	public String form(){
		System.out.println("get2");
		return formViewName;
	}
	@ModelAttribute("milegeCommand")
	public MemberMilegeCommand formBacking1() {
		return new MemberMilegeCommand();
	}
	@ModelAttribute("memberCommand")
	public MemberCommand formBacking2() {
		return new MemberCommand();
	}
	@ModelAttribute("bankSelect")
	protected List<String> bankSelect() throws Exception{
		List<String> bankSelect =new ArrayList<String>();
		
		bankSelect.add("은행선택");
		bankSelect.add("SC은행");
		bankSelect.add("국민은행");
		bankSelect.add("기업은행");
		bankSelect.add("농협");
		bankSelect.add("새마을금고");
		bankSelect.add("수협");
		bankSelect.add("신한은행");
		bankSelect.add("신협");
		bankSelect.add("씨티은행");
		bankSelect.add("외환은행");
		bankSelect.add("우리은행");
		bankSelect.add("우체국");
		bankSelect.add("하나은행");
		bankSelect.add("현대스위스저축은행");
		
		return bankSelect;
	}
	@RequestMapping(value="/main/paymentmilege.do", method=RequestMethod.POST)
	public String submit(@ModelAttribute("milegeCommand")MemberMilegeCommand milegeCommand, BindingResult result,
					     @ModelAttribute("memberCommand")MemberCommand memberCommand){

		if(result.hasErrors()){
			return formViewName;
		}
		System.out.println("milegeCommand" + milegeCommand);
		
		memberDao.insertMilege(milegeCommand);
		HashMap<String,Object> map = new HashMap<String, Object>();
		map.put("upoint",milegeCommand.getMc_charge());
		map.put("userid",milegeCommand.getUserid());
		try{
			memberDao.insertMemberpoint(map);
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return "redirect:/main/index.do";
	}
	
//=========================================================================================================
			
	
}
